package com.anti.modular.bus.roomfurniture.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.roomfurniture.entity.RoomFurniture;
import com.anti.modular.bus.roomfurniture.enums.RoomFurnitureExceptionEnum;
import com.anti.modular.bus.roomfurniture.param.RoomFurnitureParam;
import com.anti.modular.bus.roomfurniture.result.LeaseFurnitureResult;
import com.anti.modular.bus.roomfurniture.result.RoomFurnitureResult;
import com.anti.modular.bus.roomfurniture.service.RoomFurnitureService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房间家私配置表service接口实现类
 *
 * @author anti
 * @date 2021-04-18 17:08:44
 */
@Service
public class RoomFurnitureServiceImpl implements RoomFurnitureService {
    @Resource
    DBUtils dbUtils;

    @Override
    public RoomFurniture getById(RoomFurnitureParam roomFurnitureParam){
        String sql="select * from bus_room_furniture where id =? ";
        return dbUtils.get(sql,RoomFurniture.class,roomFurnitureParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,RoomFurnitureParam roomFurnitureParam) {
        StringBuilder sb=new StringBuilder("select * from bus_room_furniture where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(roomFurnitureParam)) {
            // 根据家私名称 查询
            if (ObjectUtil.isNotEmpty(roomFurnitureParam.getName())) {
               sb.append(" and name = ?");
               param.add(roomFurnitureParam.getName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<RoomFurniture> list(RoomFurnitureParam roomFurnitureParam) {
        String sql="";
        return dbUtils.query(sql,RoomFurniture.class);
    }

    @Override
    public List<RoomFurnitureResult> getRoomFurnitureListByRoomId(Long roomId) {
        String sql="select * from bus_room_furniture where roomid =? and status =0";
        List<RoomFurnitureResult> furnitureResults = dbUtils.query(sql, RoomFurnitureResult.class, roomId);
        furnitureResults.forEach(item->{
            item.setKey(item.getId()+"");
        });
        return furnitureResults;
    }

    @Override
    public List<LeaseFurnitureResult> getRoomFurnitureListByLeaseId(Long leaseId) {
        String sql="select * from bus_lease_furniture where lease_id =? and status =0";
        List<LeaseFurnitureResult> furnitureResults = dbUtils.query(sql, LeaseFurnitureResult.class, leaseId);
        furnitureResults.forEach(item->{
            item.setKey(item.getId()+"");
        });
        return furnitureResults;
    }

    @Override
    public void add(RoomFurnitureParam roomFurnitureParam) {
        RoomFurniture roomFurniture = new RoomFurniture();
        BeanUtil.copyProperties(roomFurnitureParam, roomFurniture);
        dbUtils.insert(roomFurniture);
    }

    @Override
    public void addEntity(RoomFurniture roomFurniture) {
        dbUtils.insert(roomFurniture);
    }

    public void delete(RoomFurnitureParam roomFurnitureParam) {
        String sql="delete  from bus_room_furniture where id =? ";
        dbUtils.execSql(sql,roomFurnitureParam.getId());
    }

    @Override
    public void deleteJsByRoomId(Long roomId) {
        String sql="update bus_room_furniture set update_time=?,update_user=?,status=? where roomid =? and status =0";
        dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),2,roomId);
    }

    public void edit(RoomFurnitureParam roomFurnitureParam) {
        RoomFurniture roomFurniture = this.queryRoomFurniture(roomFurnitureParam);
        BeanUtil.copyProperties(roomFurnitureParam, roomFurniture);
        dbUtils.update(roomFurniture);
    }

    @Override
    public RoomFurniture detail(RoomFurnitureParam roomFurnitureParam) {
        return this.queryRoomFurniture(roomFurnitureParam);
    }

    /**
     * 获取房间家私配置表
     *
     * @author anti
     * @date 2021-04-18 17:08:44
     */
    private RoomFurniture queryRoomFurniture(RoomFurnitureParam roomFurnitureParam) {
        RoomFurniture roomFurniture = this.getById(roomFurnitureParam);
        if (ObjectUtil.isNull(roomFurniture)) {
            throw new BusinessException(RoomFurnitureExceptionEnum.NOT_EXIST);
        }
        return roomFurniture;
    }


}
